package rsj.api.report.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import common.statusenum.OrgTypeEnum;
import common.util.CalculateUtil;
import lombok.Data;

import java.util.Objects;
import java.util.Optional;

/**
 * 账号产品报表详情
 *
 * @author yuanqinglong
 * @since 2021/7/27 16:03
 */
@Data
public class AccountProductReportDetail{

    /**
     * 统计日期
     */
    @ExcelProperty("统计时间")
    private String statDate;
    /**
     * 账号标识
     */
    @ExcelProperty(value = "账号标识")
    private String autKeyid;
    /**
     * 机构类别(正式1，测试2)
     */
    @ExcelProperty(value = "账号类型")
    private String orgType;
    /**
     * 客户id
     */
    @ExcelProperty(value = "客户编号")
    private String custId;
    /**
     * 客户简称
     */
    @ExcelProperty(value = "客户简称")
    private String custSname;
    /**
     * 接口uid
     */
    @ExcelProperty(value = "接口uid")
    private String prodUid;
    /**
     * 接口名称
     */
    @ExcelProperty(value = "接口名称")
    private String prodName;
    /**
     * 请求数
     */
    @ExcelProperty(value = "请求数")
    private Integer statReqNum;
    /**
     * 查询数
     */
    @ExcelProperty(value = "查询数")
    private Integer statQueryNum;
    /**
     * 查询率
     */
    @ExcelProperty(value = "查询率")
    private String statQueryRate;
    /**
     * 查得数
     */
    @ExcelProperty(value = "查得数")
    private Integer statFindoutNum;
    /**
     * 查得率
     */
    @ExcelProperty(value = "查得率")
    private String statFindoutRate;
    /**
     * 计费数
     */
    @ExcelProperty(value = "计费数")
    private Integer statChargeNum;
    /**
     * 计费率
     */
    @ExcelProperty(value = "计费率")
    private String statChargeRate;
    /**
     * 失败数
     */
    @ExcelProperty(value = "失败数")
    private Integer statFailNum;
    /**
     * 失败率
     */
    @ExcelProperty(value = "失败率")
    private String statFailRate;




    /**
     * 处理属性值
     *
     * @param reportParam 报告参数
     */
    public void processingAttributeValue(ReportParam reportParam){
        orgType = OrgTypeEnum.getMsgByCode(orgType);
        this.calculateRate();
    }

    /**
     * 计算率
     */
    public void calculateRate() {
        if(statReqNum == 0){
            statChargeRate = CalculateUtil.getPercent(0.00);
            statFailRate = CalculateUtil.getPercent(0.00);
            statFindoutRate = CalculateUtil.getPercent(0.00);
        }else {
            statChargeRate = Optional.ofNullable(statChargeNum).map(integer -> CalculateUtil.getPercent(statChargeNum / (double) statReqNum)).orElse(null);
            statFailRate = Optional.ofNullable(statFailNum).map(integer -> CalculateUtil.getPercent(statFailNum / (double) statReqNum)).orElse(null);
            statFindoutRate = Optional.ofNullable(statFindoutNum).map(integer -> CalculateUtil.getPercent(statFindoutNum / (double) statReqNum)).orElse(null);
        }
        this.calculateQueryRate();
    }

    /**
     * 计算查询率
     */
    private void calculateQueryRate() {
        if (Objects.nonNull(statReqNum) && Objects.nonNull(statFailNum)) {
            if(statReqNum == 0){
                statQueryRate = CalculateUtil.getPercent(0.00);
            }else {
                statQueryNum = statReqNum - statFailNum;
                statQueryRate = CalculateUtil.getPercent(statQueryNum / (double) statReqNum);
            }
        }
    }

}
